<!DOCTYPE html>
<html>
<head>
	<title>发布</title>
    <link rel="stylesheet" href="../js/default.css" />
    <script charset="utf-8" src="../js/kindeditor-all-min.js"></script>
    <script charset="utf-8" src="../js/zh_CN.js"></script>
    <script>
        var editor;
        KindEditor.ready(function(K) {
            editor = K.create('textarea[name="content"]', {
                allowFileManager : true
            });
            K('input[name=getHtml]').click(function(e) {
                alert(editor.html());
            });
            K('input[name=isEmpty]').click(function(e) {
                alert(editor.isEmpty());
            });
            K('input[name=getText]').click(function(e) {
                alert(editor.text());
            });
            K('input[name=selectedHtml]').click(function(e) {
                alert(editor.selectedHtml());
            });
            K('input[name=setHtml]').click(function(e) {
                editor.html('<h3>Hello KindEditor</h3>');
            });
            K('input[name=setText]').click(function(e) {
                editor.text('<h3>Hello KindEditor</h3>');
            });
            K('input[name=insertHtml]').click(function(e) {
                editor.insertHtml('<strong>插入HTML</strong>');
            });
            K('input[name=appendHtml]').click(function(e) {
                editor.appendHtml('<strong>添加HTML</strong>');
            });
            K('input[name=clear]').click(function(e) {
                editor.html('');
            });
        });
    </script>	
	<style type="text/css">
	.div1
		{
			padding: 10px ;
			width: 1000px;
			margin: 0 auto;
			border: solid 1px black;
			background-color:lightgray;
			text-align: center;		
		}
		.t1 
		{
			line-height: 30px;padding: 20px 100px ;
		}
		.align_r
		{
			text-align: left;width: 30px;height: 30px;white-space: pre-wrap;
		}
		.align_c
		{
			padding-left: 60px ;
			text-align: center;
		}		    
	</style>
</head>
<body>
	<div class="div1">	 
		<span>新添章节</span>
		<form action="" method="post">
			<input type="hidden" name="book_id" value="<?php echo $_GET['id'];?>">
		<table class="t1">
			<tr><td class=align_r>标题</td><td><input name="title" style="width:700px;height:30px;"></td><tr>
			<tr><td class=align_r>内容</td><td><textarea id="mul_input" name="content" style="width:700px;height:500px;visibility:hidden;display: block;"></textarea></td><tr>

			<p style="margin: 0 auto;margin-top: 5px;">               
                <input type="button" name="clear" value="清空内容" />               
            </p>			
			<tr><td class=align_c colspan ="2"><input type="submit" name="add" value="提交"></td><tr>
		</table>
		</form>
		<?php
		if (isset($_POST["add"]))
		{
			require_once "config.php";
			$book_id=$_POST['book_id'];
			$title=$_POST["title"];
			$content=$_POST["content"];
			$datetime=date("Y-m-d H:i:s");
			$title_id="select * from book_article where book_id='$book_id' order by id desc limit 1";
			$title_id2="select * from book_article order by id desc limit 1";
			$data=mysqli_query($connect,$title_id);
			$data2=mysqli_query($connect,$title_id2);
			$num_rows=mysqli_num_rows($data);
			if($num_rows==0){
				while ($out2=mysqli_fetch_array($data2)) {
				    $up_id=$out2[0]+1;
				    $next_id=0;
					$sql="insert into book_article(book_id,title,content,up_id,next_id,datetime)
					values('$book_id','$title','$content','$up_id','$next_id','$datetime')";
					if (mysqli_query($connect,$sql)) 
						echo "<script>alert('发布成功!');</script>";
					else
						echo "<script>alert('发布失败！');</script>";
				}				
			}
			else{
				while ($out=mysqli_fetch_array($data) and $out2=mysqli_fetch_array($data2)) {			
				$next_id2=$out2[0]+1;	
				$ti_id=$out[0];
			    $up_id=$out[0];
			    $next_id=0;
				$datetime=date("Y-m-d H:i:s");
				$sql1 = "UPDATE book_article SET next_id='$next_id2' where id='$ti_id'";

				
				$sql2="insert into book_article(book_id,title,content,up_id,next_id,datetime)
				values('$book_id','$title','$content','$up_id','$next_id','$datetime')";
				$a=mysqli_query($connect,$sql1);
				$b=mysqli_query($connect,$sql2);
				if ($a!=0 AND $b!=0) 
					echo "<script>alert('发布成功!');this.location.href='http://www.1.com/project/writer/writing.php?id=$book_id';</script>";
				else
					echo "<script>alert('发布失败！');</script>";
				}

			}
		mysqli_close($connect);
			
		}
		?>
	</div>
</body>
</html>